package com.dianban.controller.nengliang;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dianban.base.ResultCode;
import com.dianban.base.ResultVo;
import com.dianban.config.redis.RedisConstant;
import com.dianban.config.redis.RedisUtil;
import com.dianban.controller.BaseController;
import com.dianban.entity.*;
import com.dianban.service.*;
import com.dianban.utils.DateUtils;
import com.dianban.utils.UserUtils;
import com.dianban.utils.http.HttpClient;
import com.dianban.utils.http.HttpMethod;
import com.dianban.utils.http.HttpParamers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wujh
 * @create 2020/1/10
 */
@Api(value = "ApiOrderController", description = "订单控制器")
@RestController
@RequestMapping(value = "${interfacePath}/nengliang/order/")
public class ApiOrderController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private WxUserService wxUserService;
    @Autowired
    private FacePayStoreOrderService facePayStoreOrderService;
    @Autowired
    private FacePayStoreConsumeLogService facePayStoreConsumeLogService;
    @Autowired
    private StoreGuideService storeGuideService;
    @Autowired
    private ShoproOrderService shoproOrderService;
    @Autowired
    private ShopUserService shopUserService;
    @Autowired
    private JwShoproGoodsService jwShoproGoodsService;
    @Autowired
    private JwStoreService storeService;
    @Autowired
    private FacePayConfigService facePayConfigService;
    @Autowired
    private FacePayStoreOrderShareLogService facePayStoreOrderShareLogService;

    // 获取统计详细列表
    @ApiOperation(notes = "statistics", httpMethod = "GET", value = "获取我的门店订单统计详细列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
    })
    @RequestMapping(value = "statistics", method = RequestMethod.GET)
    public ResultVo statistics(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            String userToken = request.getHeader(UserUtils.USER_TOKEN);
            String key = RedisConstant.USER_TOKEN_KEY + userToken;
            User user = (User) RedisUtil.get(key);
            if (user == null) {
                return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
            }
            User realUser = userService.findById(user.getId());
            String storeIdStr = realUser.getStoreIds();
            if (StringUtils.isEmpty(storeIdStr)) {
                return ResultVo.error(ResultCode.ERROR, "系统未授予当前用户查询权限,无法进行查询");
            }
            String[] storeIds = storeIdStr.split(",");
            //String[] storeIds = new String[]{"48", "50", "57"};
            List<String> storeIdList = new ArrayList<>(Arrays.asList(storeIds));
            // 获取分配的用户门店
            List<Map<String, Object>> list = facePayStoreOrderService.statistics(storeIdList);
            // 门店名称,今日订单数,今日订单总额,全部订单数,全部订单总额,已核销次数,剩余次数
            Map<String, Object> statisticsMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(list)) {
                Integer storeOrderTotalCount = 0;
                BigDecimal storeOrderTotalAmount = BigDecimal.ZERO;
                for (Map<String, Object> map : list) {
                    storeOrderTotalCount += Integer.parseInt(String.valueOf(map.get("totalCount")));
                    storeOrderTotalAmount = storeOrderTotalAmount.add(new BigDecimal(String.valueOf(map.get("totalAmount"))));
                }
                // 循环处理,计算总的门店数量,总订单数,总金额
                statisticsMap.put("storeCount", storeIds.length);
                statisticsMap.put("storeOrderTotalCount", storeOrderTotalCount);
                statisticsMap.put("storeOrderTotalAmount", storeOrderTotalAmount.setScale(2, BigDecimal.ROUND_HALF_DOWN));
            }
            resultMap.put("statisticsList", list);
            resultMap.put("statisticsMap", statisticsMap);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return ResultVo.success(resultMap, "成功");
    }

    // 获取我的订单列表
    @ApiOperation(notes = "list", httpMethod = "GET", value = "我的订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
    })
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVo list(HttpServletRequest request, HttpServletResponse response) {
        String userToken = request.getHeader(UserUtils.USER_TOKEN);
        String key = RedisConstant.USER_TOKEN_KEY + userToken;
        User user = (User) RedisUtil.get(key);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
        }
        List<FacePayStoreOrder> facePayStoreOrderList = facePayStoreOrderService.findByParams(null, null, user.getId(), null, null, "20", null);
        return ResultVo.success(facePayStoreOrderList, "操作成功");
    }

    // 获取我的订单套餐使用情况列表(门店名称 门店设备在线状态 剩余次数 已使用次数 分享中 分享成功)
    @ApiOperation(notes = "orderUselist", httpMethod = "GET", value = "获取我的订单套餐使用情况列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
            @ApiImplicitParam(name = "type", value = "套餐类型 10线上APP 20线下门店", required = true, paramType = "query", dataType = "string"),
    })
    @RequestMapping(value = "orderUselist", method = RequestMethod.GET)
    public ResultVo orderUselist(String type, HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, JSONArray> dataMap = new LinkedHashMap<>();
        try {
            String userToken = request.getHeader(UserUtils.USER_TOKEN);
            String key = RedisConstant.USER_TOKEN_KEY + userToken;
            User user = (User) RedisUtil.get(key);
            if (user == null) {
                return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
            }
            // TODO：门店名称 门店设备在线状态 剩余次数 已使用次数 分享中 分享成功
            // TODO: 剩余次数未0的不展示
            // TODO: 按门店-套餐情况,map返回,如果设备没在线马上使用置灰
            // 门店名称查询出来
            List<JwStore> storeList = storeService.findList("");
            Map<String, String> storeStatusMap = new HashMap<>();
            Map<Long, String> storeMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(storeList)) {
                for (JwStore jwStore : storeList) {
                    storeMap.put(jwStore.getId(), jwStore.getName());
                }
                for (JwStore jwStore : storeList) {
                    if (StringUtils.isEmpty(storeStatusMap.get(storeMap.get(jwStore.getId())))) {
                        String onlineStatus = "0";
                        // 查询门店设备在线状态
                        List<FacePayConfig> facePayConfigList = facePayConfigService.find(20, null, jwStore.getId());
                        if (!facePayConfigList.isEmpty()) {
                            // sn号,查询设备是否在线
                            String name = facePayConfigList.get(0).getName();
                            String url = "http://120.24.76.21:8080/iot-admin/app/modbus/equipStatusCheck?sn=" + name;
                            HttpParamers httpParamers = new HttpParamers(HttpMethod.GET);
                            String resultService = HttpClient.doService(url, httpParamers, null);
                            if (StringUtils.isNotBlank(resultService)) {
                                JSONObject jsonObject = JSONObject.parseObject(resultService);
                                if (jsonObject != null && jsonObject.get("code") != null
                                        && "0".equals(String.valueOf(jsonObject.get("code")))) {
                                    onlineStatus = String.valueOf(jsonObject.get("onlineStatus"));
                                }
                            }
                        }
                        storeStatusMap.put(storeMap.get(jwStore.getId()), onlineStatus);
                    }
                }
            }
            if ("20".equals(type)) {
                List<FacePayStoreOrder> facePayStoreOrderList = facePayStoreOrderService.findByParams(null, null, user.getId(), null, null, "20", null);
                if (!CollectionUtils.isEmpty(facePayStoreOrderList)) {
                    for (FacePayStoreOrder facePayStoreOrder : facePayStoreOrderList) {
                        Integer canVerifyCount = facePayStoreOrder.getCanVerifyCount();
                        Integer hasVerifyCount = facePayStoreOrder.getHasVerifyCount();
                        if (canVerifyCount.compareTo(hasVerifyCount) > 0) {
                            if (dataMap.get(storeMap.get(facePayStoreOrder.getStoreId())) == null) {
                                JSONArray jsonArray = new JSONArray();
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("shareLogId", "");
                                jsonObject.put("orderId", facePayStoreOrder.getId());
                                jsonObject.put("storeId", facePayStoreOrder.getStoreId());
                                jsonObject.put("storeName", storeMap.get(facePayStoreOrder.getStoreId()));
                                // 已使用
                                jsonObject.put("hasUseCount", facePayStoreOrder.getHasVerifyCount());
                                // 剩余可使用
                                jsonObject.put("remainUseCount", (facePayStoreOrder.getCanVerifyCount() - facePayStoreOrder.getHasVerifyCount()));
                                // 分享中
                                jsonObject.put("sharingCount", facePayStoreOrder.getSharingCount());
                                // 分享成功
                                jsonObject.put("hasShareCount", facePayStoreOrder.getHasShareCount());
                                // 查询设备是否在线
                                //jsonObject.put("isOnline", onlineStatus);
                                jsonObject.put("money", facePayStoreOrder.getMoney());
                                jsonObject.put("type", "buy");
                                jsonArray.add(jsonObject);
                                dataMap.put(storeMap.get(facePayStoreOrder.getStoreId()), jsonArray);
                            } else {
                                JSONArray jsonArray = dataMap.get(storeMap.get(facePayStoreOrder.getStoreId()));
                                if (!CollectionUtils.isEmpty(jsonArray)) {
                                    JSONObject jsonObject = new JSONObject();
                                    jsonObject.put("shareLogId", "");
                                    jsonObject.put("orderId", facePayStoreOrder.getId());
                                    jsonObject.put("storeId", facePayStoreOrder.getStoreId());
                                    jsonObject.put("storeName", storeMap.get(facePayStoreOrder.getStoreId()));
                                    // 已使用
                                    jsonObject.put("hasUseCount", facePayStoreOrder.getHasVerifyCount());
                                    // 剩余可使用
                                    jsonObject.put("remainUseCount", (facePayStoreOrder.getCanVerifyCount() - facePayStoreOrder.getHasVerifyCount()));
                                    // 分享中
                                    jsonObject.put("sharingCount", facePayStoreOrder.getSharingCount());
                                    // 分享成功
                                    jsonObject.put("hasShareCount", facePayStoreOrder.getHasShareCount());
                                    // 查询设备是否在线
                                    //jsonObject.put("isOnline", onlineStatus);
                                    jsonObject.put("money", facePayStoreOrder.getMoney());
                                    jsonObject.put("type", "buy");
                                    jsonArray.add(jsonObject);

                                    // 排序
                                    Collections.sort(jsonArray, new Comparator() {
                                        @Override
                                        public int compare(Object a, Object b) {
                                            JSONObject a1 = (JSONObject) a;
                                            JSONObject b1 = (JSONObject) b;
                                            return new BigDecimal(String.valueOf(a1.get("money"))).compareTo(new BigDecimal(String.valueOf(b1.get("money"))));
                                        }
                                    });

                                    dataMap.put(storeMap.get(facePayStoreOrder.getStoreId()), jsonArray);
                                }
                            }
                        }
                    }
                }
                // 将分享的也查询出来
                List<FacePayStoreOrderShareLog> facePayStoreOrderShareLogList = facePayStoreOrderShareLogService.findList("1", null, user.getId());
                if (!CollectionUtils.isEmpty(facePayStoreOrderShareLogList)) {
                    for (FacePayStoreOrderShareLog facePayStoreOrderShareLog : facePayStoreOrderShareLogList) {
                        FacePayStoreOrder facePayStoreOrder = facePayStoreOrderService.findById(facePayStoreOrderShareLog.getOrderId());
                        if (facePayStoreOrder != null) {
                            if (dataMap.get(storeMap.get(facePayStoreOrder.getStoreId())) == null) {
                                JSONArray jsonArray = new JSONArray();
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("shareLogId", facePayStoreOrderShareLog.getId());
                                jsonObject.put("orderId", facePayStoreOrder.getId());
                                jsonObject.put("storeId", facePayStoreOrder.getStoreId());
                                jsonObject.put("storeName", storeMap.get(facePayStoreOrder.getStoreId()));
                                // 已使用
                                jsonObject.put("hasUseCount", "0");
                                // 剩余可使用
                                jsonObject.put("remainUseCount", "1");
                                // 分享中
                                jsonObject.put("sharingCount", "0");
                                // 分享成功
                                jsonObject.put("hasShareCount", "0");
                                // 查询设备是否在线
                                //jsonObject.put("isOnline", onlineStatus);
                                jsonObject.put("money", facePayStoreOrder.getMoney());
                                jsonObject.put("type", "share");
                                jsonArray.add(jsonObject);
                                dataMap.put(storeMap.get(facePayStoreOrder.getStoreId()), jsonArray);
                            } else {
                                JSONArray jsonArray = dataMap.get(storeMap.get(facePayStoreOrder.getStoreId()));
                                if (!CollectionUtils.isEmpty(jsonArray)) {
                                    JSONObject jsonObject = new JSONObject();
                                    jsonObject.put("shareLogId", facePayStoreOrderShareLog.getId());
                                    jsonObject.put("orderId", facePayStoreOrder.getId());
                                    jsonObject.put("storeId", facePayStoreOrder.getStoreId());
                                    jsonObject.put("storeName", storeMap.get(facePayStoreOrder.getStoreId()));
                                    // 已使用
                                    jsonObject.put("hasUseCount", "0");
                                    // 剩余可使用
                                    jsonObject.put("remainUseCount", "1");
                                    // 分享中
                                    jsonObject.put("sharingCount", "0");
                                    // 分享成功
                                    jsonObject.put("hasShareCount", "0");
                                    // 查询设备是否在线
                                    //jsonObject.put("isOnline", onlineStatus);
                                    jsonObject.put("money", facePayStoreOrder.getMoney());
                                    jsonObject.put("type", "share");
                                    jsonArray.add(jsonObject);

                                    // 排序
                                    Collections.sort(jsonArray, new Comparator() {
                                        @Override
                                        public int compare(Object a, Object b) {
                                            JSONObject a1 = (JSONObject) a;
                                            JSONObject b1 = (JSONObject) b;
                                            return new BigDecimal(String.valueOf(a1.get("money"))).compareTo(new BigDecimal(String.valueOf(b1.get("money"))));
                                        }
                                    });

                                    dataMap.put(storeMap.get(facePayStoreOrder.getStoreId()), jsonArray);
                                }
                            }
                        }
                    }
                }
            } else if ("10".equals(type)) {
                List<ShoproOrder> shoproOrderList500 = shoproOrderService.findVerifyList(user.getId(), ShoproOrder.goodsId_500, null);
                List<ShoproOrder> shoproOrderList5000 = shoproOrderService.findVerifyList(user.getId(), ShoproOrder.goodsId_5000, null);
                List<ShoproOrder> onlineOrderList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(shoproOrderList500)) {
                    onlineOrderList.addAll(shoproOrderList500);
                }
                if (!CollectionUtils.isEmpty(shoproOrderList5000)) {
                    onlineOrderList.addAll(shoproOrderList5000);
                }
                if (!CollectionUtils.isEmpty(onlineOrderList)) {
                    for (ShoproOrder shoproOrder : onlineOrderList) {
                        if (shoproOrder.getStoreId() == null) {
                            //return ResultVo.error(ResultCode.ERROR, "该订单暂未配置到具体门店,请联系管理员处理");
                            continue;
                        }
                        Integer canVerifyCount = shoproOrder.getCanVerifyCount();
                        Integer hasVerifyCount = shoproOrder.getHasVerifyCount();
                        if (canVerifyCount.compareTo(hasVerifyCount) > 0) {
                            if (StringUtils.isEmpty(storeStatusMap.get(storeMap.get(shoproOrder.getStoreId())))) {
                                String onlineStatus = "0";
                                // 查询门店设备在线状态
                                List<FacePayConfig> facePayConfigList = facePayConfigService.find(20, null, shoproOrder.getStoreId());
                                if (!facePayConfigList.isEmpty()) {
                                    // sn号,查询设备是否在线
                                    String name = facePayConfigList.get(0).getName();
                                    String url = "http://120.24.76.21:8080/iot-admin/app/modbus/equipStatusCheck?sn=" + name;
                                    HttpParamers httpParamers = new HttpParamers(HttpMethod.GET);
                                    String resultService = HttpClient.doService(url, httpParamers, null);
                                    if (StringUtils.isNotBlank(resultService)) {
                                        JSONObject jsonObject = JSONObject.parseObject(resultService);
                                        if (jsonObject != null && jsonObject.get("code") != null
                                                && "0".equals(String.valueOf(jsonObject.get("code")))) {
                                            onlineStatus = String.valueOf(jsonObject.get("onlineStatus"));
                                        }
                                    }
                                }
                                storeStatusMap.put(storeMap.get(shoproOrder.getStoreId()), onlineStatus);
                            }
                            if (dataMap.get(storeMap.get(shoproOrder.getStoreId())) == null) {
                                JSONArray jsonArray = new JSONArray();
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("shareLogId", "");
                                jsonObject.put("orderId", shoproOrder.getId());
                                jsonObject.put("storeId", shoproOrder.getStoreId());
                                jsonObject.put("storeName", storeMap.get(shoproOrder.getStoreId()));
                                // 已使用
                                jsonObject.put("hasUseCount", shoproOrder.getHasVerifyCount());
                                // 剩余可使用
                                jsonObject.put("remainUseCount", (shoproOrder.getCanVerifyCount() - shoproOrder.getHasVerifyCount()));
                                // 分享中
                                jsonObject.put("sharingCount", "0");
                                // 分享成功
                                jsonObject.put("hasShareCount", "0");
                                // 查询设备是否在线
                                //jsonObject.put("isOnline", onlineStatus);
                                jsonObject.put("money", shoproOrder.getTotalAmount());
                                jsonObject.put("type", "buy");
                                jsonArray.add(jsonObject);
                                dataMap.put(storeMap.get(shoproOrder.getStoreId()), jsonArray);
                            } else {
                                JSONArray jsonArray = dataMap.get(storeMap.get(shoproOrder.getStoreId()));
                                if (!CollectionUtils.isEmpty(jsonArray)) {
                                    JSONObject jsonObject = new JSONObject();
                                    jsonObject.put("shareLogId", "");
                                    jsonObject.put("orderId", shoproOrder.getId());
                                    jsonObject.put("storeId", shoproOrder.getStoreId());
                                    jsonObject.put("storeName", storeMap.get(shoproOrder.getStoreId()));
                                    // 已使用
                                    jsonObject.put("hasUseCount", shoproOrder.getHasVerifyCount());
                                    // 剩余可使用
                                    jsonObject.put("remainUseCount", (shoproOrder.getCanVerifyCount() - shoproOrder.getHasVerifyCount()));
                                    // 分享中
                                    jsonObject.put("sharingCount", "0");
                                    // 分享成功
                                    jsonObject.put("hasShareCount", "0");
                                    // 查询设备是否在线
                                    //jsonObject.put("isOnline", onlineStatus);
                                    jsonObject.put("money", shoproOrder.getTotalAmount());
                                    jsonObject.put("type", "buy");
                                    jsonArray.add(jsonObject);

                                    // 排序
                                    Collections.sort(jsonArray, new Comparator() {
                                        @Override
                                        public int compare(Object a, Object b) {
                                            JSONObject a1 = (JSONObject) a;
                                            JSONObject b1 = (JSONObject) b;
                                            return new BigDecimal(String.valueOf(a1.get("money"))).compareTo(new BigDecimal(String.valueOf(b1.get("money"))));
                                        }
                                    });

                                    dataMap.put(storeMap.get(shoproOrder.getStoreId()), jsonArray);
                                }
                            }
                        }
                    }
                }
            }
            if (dataMap != null) {
                for (String dataKey : dataMap.keySet()) {
                    Map<String, Object> resultData = new HashMap<>();
                    resultData.put("name", dataKey);
                    //resultData.put("isOnline", JSONObject.parseObject(String.valueOf(dataMap.get(dataKey).get(0))).get("isOnline"));
                    resultData.put("isOnline", storeStatusMap.get(dataKey));
                    resultData.put("list", dataMap.get(dataKey));
                    resultList.add(resultData);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return ResultVo.success(resultList, "查询成功");
    }

    // TODO：可以根据选择的订单进行核销,增加核销的方案,直接通过订单id进行核销
    @ApiOperation(notes = "orderVerify", httpMethod = "POST", value = "能量舱使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
            @ApiImplicitParam(name = "orderId", value = "订单id", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "verifyType", value = "核销类型 10线上APP 20线下门店", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "shareLogId", value = "分享id", required = false, paramType = "query", dataType = "string"),
    })
    @RequestMapping(value = "orderVerify", method = RequestMethod.POST)
    public ResultVo orderVerify(Long orderId, String verifyType,
                                @RequestParam(value = "shareLogId", required = false, defaultValue = "") String shareLogId,
                                HttpServletRequest request, HttpServletResponse response) {
        String userToken = request.getHeader(UserUtils.USER_TOKEN);
        String key = RedisConstant.USER_TOKEN_KEY + userToken;
        User user = (User) RedisUtil.get(key);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
        }
        if (orderId == null) {
            return ResultVo.error(ResultCode.ERROR, "订单id不能为空");
        }
        Long userId = user.getId();
        FacePayStoreConsumeLog facePayStoreConsumeLog = new FacePayStoreConsumeLog();
        facePayStoreConsumeLog.setUserId(userId);
        if (StringUtils.isNotBlank(shareLogId)) {
            FacePayStoreOrderShareLog shareLog = facePayStoreOrderShareLogService.findById(Long.valueOf(shareLogId));
            if (shareLog != null && shareLog.getGetUserId() != null && userId.equals(shareLog.getGetUserId())) {
                FacePayStoreOrder facePayStoreOrder = facePayStoreOrderService.findById(shareLog.getOrderId());
                facePayStoreConsumeLog.setMoney(facePayStoreOrder.getMoney());
                facePayStoreConsumeLog.setType("60");
                facePayStoreConsumeLog.setOnlineOrderId(shareLog.getOrderId());
                facePayStoreConsumeLog.setShareLogId(Long.valueOf(shareLogId));
                facePayStoreConsumeLog.setStoreId(facePayStoreOrder.getStoreId());
                facePayStoreConsumeLog.setMemo("分享" + facePayStoreOrder.getMoney() + "套餐核销");
                return facePayStoreConsumeLogService.verifyShare(facePayStoreConsumeLog);
            } else {
                return ResultVo.error(ResultCode.ERROR, "不存在该分享订单");
            }
        } else {
            if ("10".equals(verifyType)) {
                ShoproOrder shoproOrder = shoproOrderService.findById(orderId);
                if (shoproOrder != null && shoproOrder.getUserId().equals(userId)) {
                    // 多一层验证判断下次数
                    Integer canVerifyCount = shoproOrder.getCanVerifyCount();
                    Integer hasVerifyCount = shoproOrder.getHasVerifyCount();
                    if (canVerifyCount == null || hasVerifyCount == null || ((hasVerifyCount + 1) > canVerifyCount)) {
                        return ResultVo.error(ResultCode.ERROR, "订单剩余可用次数为0");
                    }
                } else {
                    return ResultVo.error(ResultCode.ERROR, "线下订单不存在");
                }
                BigDecimal money = shoproOrder.getTotalAmount();
                facePayStoreConsumeLog.setMoney(money);
                facePayStoreConsumeLog.setType(verifyType);
                facePayStoreConsumeLog.setOnlineOrderId(orderId);
                facePayStoreConsumeLog.setStoreId(shoproOrder.getStoreId());
                ShopUser shopUser = shopUserService.findByStoreId(shoproOrder.getStoreId());
                Long shopUserId = shopUser.getId();
                facePayStoreConsumeLog.setMemo("线上" + money + "套餐核销");
                return facePayStoreConsumeLogService.verify(facePayStoreConsumeLog, shopUserId);
            } else if ("20".equals(verifyType)) {
                FacePayStoreOrder facePayStoreOrder = facePayStoreOrderService.findById(orderId);
                if (facePayStoreOrder != null) {
                    // 多一层验证判断下次数
                    Integer canVerifyCount = facePayStoreOrder.getCanVerifyCount();
                    Integer hasVerifyCount = facePayStoreOrder.getHasVerifyCount();
                    if (canVerifyCount == null || hasVerifyCount == null || ((hasVerifyCount + 1) > canVerifyCount)) {
                        return ResultVo.error(ResultCode.ERROR, "订单剩余可用次数为0");
                    }
                } else {
                    return ResultVo.error(ResultCode.ERROR, "门店订单不存在");
                }
                BigDecimal money = facePayStoreOrder.getMoney();
                facePayStoreConsumeLog.setMoney(money);
                facePayStoreConsumeLog.setType(verifyType);
                facePayStoreConsumeLog.setOfflineOrderId(orderId);
                facePayStoreConsumeLog.setStoreId(facePayStoreOrder.getStoreId());
                ShopUser shopUser = shopUserService.findByStoreId(facePayStoreOrder.getStoreId());
                Long shopUserId = shopUser.getId();
                facePayStoreConsumeLog.setMemo("线下" + money + "体验套餐核销");
                return facePayStoreConsumeLogService.verify(facePayStoreConsumeLog, shopUserId);
            }
        }
        return ResultVo.error(ResultCode.ERROR, "能量舱使用核销类型错误");
    }

    @ApiOperation(notes = "verifyList", httpMethod = "GET", value = "获取核销列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
            @ApiImplicitParam(name = "storeId", value = "店铺ID", required = true, paramType = "query", dataType = "string"),
    })
    @RequestMapping(value = "verifyList", method = RequestMethod.GET)
    public ResultVo verifyList(Long storeId, HttpServletRequest request, HttpServletResponse response) {
        String userToken = request.getHeader(UserUtils.USER_TOKEN);
        String key = RedisConstant.USER_TOKEN_KEY + userToken;
        User user = (User) RedisUtil.get(key);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
        }
        List<Map<String, Object>> verifyList = new ArrayList<>();
        // APP剩余次数
        Integer verifyCount = 0;
        Map<String, Object> result = userService.findVerify("10", user, ShoproOrder.goodsId_500, null, "10", "500");
        verifyCount = verifyCount + (Integer) result.get("verifyCount");
        result = userService.findVerify("10", user, ShoproOrder.goodsId_5000, null, "10", "5000");
        verifyCount = verifyCount + (Integer) result.get("verifyCount");
        Map<String, Object> verify = new HashMap<>();
        verify.put("verifyTitle", "APP剩余次数");
        verify.put("verifyType", "10");
        verify.put("verifyCount", verifyCount);
        verifyList.add(verify);
        // 门店剩余次数
        verifyCount = 0;
        result = userService.findVerify("20", user, null, storeId, "20", null);
        verifyCount = verifyCount + (Integer) result.get("verifyCount");
        verify = new HashMap<>();
        verify.put("verifyTitle", "门店剩余次数");
        verify.put("verifyType", "20");
        verify.put("verifyCount", verifyCount);
        verifyList.add(verify);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("verifyList", verifyList);
        return ResultVo.success(resultMap, "成功");
    }

    @ApiOperation(notes = "verify", httpMethod = "POST", value = "核销")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
            @ApiImplicitParam(name = "storeId", value = "店铺ID", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "verifyType", value = "核销类型", required = true, paramType = "query", dataType = "string"),
    })
    @RequestMapping(value = "verify", method = RequestMethod.POST)
    public ResultVo verify(Long storeId, String verifyType, HttpServletRequest request, HttpServletResponse response) {
        String userToken = request.getHeader(UserUtils.USER_TOKEN);
        String key = RedisConstant.USER_TOKEN_KEY + userToken;
        User user = (User) RedisUtil.get(key);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
        }
        if (storeId == null) {
            return ResultVo.error(ResultCode.ERROR, "店铺ID不能为空");
        }
        ShopUser shopUser = shopUserService.findByStoreId(storeId);
        Long shopUserId = shopUser.getId();
        Integer verifyCount = 0;
        Long userId = user.getId();
        FacePayStoreConsumeLog facePayStoreConsumeLog = new FacePayStoreConsumeLog();
        facePayStoreConsumeLog.setStoreId(storeId);
        facePayStoreConsumeLog.setUserId(userId);
        if ("10".equals(verifyType)) {
            if (verifyCount == 0) {
                Map<String, Object> verifyResult = userService.findVerify(verifyType, user, ShoproOrder.goodsId_500, null, "10", "500");
                verifyCount = (Integer) verifyResult.get("verifyCount");
                if (verifyCount > 0) {
                    List<Long> orderIdList = (List) verifyResult.get("orderIdList");
                    ShoproOrder shoproOrder = shoproOrderService.findById(orderIdList.get(0));
                    BigDecimal money = shoproOrder.getTotalAmount();
                    facePayStoreConsumeLog.setMoney(money);
                    facePayStoreConsumeLog.setType(verifyType);
                    facePayStoreConsumeLog.setOnlineOrderId(orderIdList.get(0));
                    facePayStoreConsumeLog.setMemo("线上500套餐核销");
                    return facePayStoreConsumeLogService.verify(facePayStoreConsumeLog, shopUserId);
                }
            }
            if (verifyCount == 0) {
                Map<String, Object> verifyResult = userService.findVerify(verifyType, user, ShoproOrder.goodsId_5000, null, "10", "5000");
                verifyCount = (Integer) verifyResult.get("verifyCount");
                if (verifyCount > 0) {
                    List<Long> orderIdList = (List) verifyResult.get("orderIdList");
                    ShoproOrder shoproOrder = shoproOrderService.findById(orderIdList.get(0));
                    BigDecimal money = shoproOrder.getTotalAmount();
                    facePayStoreConsumeLog.setMoney(money);
                    facePayStoreConsumeLog.setType(verifyType);
                    facePayStoreConsumeLog.setOnlineOrderId(orderIdList.get(0));
                    facePayStoreConsumeLog.setMemo("线上5000套餐核销");
                    return facePayStoreConsumeLogService.verify(facePayStoreConsumeLog, shopUserId);
                }
            }
            if (verifyCount == 0) {
                return ResultVo.error(ResultCode.ERROR, "已核销完");
            }
        } else if ("20".equals(verifyType)) {
            if (verifyCount == 0) {
                Map<String, Object> verifyResult = userService.findVerify(verifyType, user, null, storeId, "20", null);
                verifyCount = (Integer) verifyResult.get("verifyCount");
                if (verifyCount > 0) {
                    List<Long> orderIdList = (List<Long>) verifyResult.get("orderIdList");
                    FacePayStoreOrder facePayStoreOrder = facePayStoreOrderService.findById(orderIdList.get(0));
                    BigDecimal money = facePayStoreOrder.getMoney();
                    facePayStoreConsumeLog.setMoney(money);
                    facePayStoreConsumeLog.setType(verifyType);
                    facePayStoreConsumeLog.setOfflineOrderId(orderIdList.get(0));
                    facePayStoreConsumeLog.setMemo("线下" + money + "体验套餐核销");
                    return facePayStoreConsumeLogService.verify(facePayStoreConsumeLog, shopUserId);
                }
            }
            if (verifyCount == 0) {
                return ResultVo.error(ResultCode.ERROR, "已核销完");
            }
        }
        return ResultVo.error(ResultCode.ERROR, "核销类型错误");
    }

    @ApiOperation(notes = "typeList", httpMethod = "GET", value = "获取订单类型列表")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "typeList", method = RequestMethod.GET)
    public ResultVo typeList(Long storeId, HttpServletRequest request, HttpServletResponse response) {
        String userToken = request.getHeader(UserUtils.USER_TOKEN);
        String key = RedisConstant.USER_TOKEN_KEY + userToken;
        User user = (User) RedisUtil.get(key);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
        }
        Long userId = user.getId();
        if (storeId == null) {
            return ResultVo.error(ResultCode.ERROR, "店铺ID不能为空");
        }
        List<Map<String, Object>> typeList = new ArrayList<>();
        Integer verifyCount = 0;
        Map<String, Object> type = null;
        String verifyName = "未购买";
        String curPaymentAmount = null;
        Map<String, Object> result = null;
        // 20 门店支付
        curPaymentAmount = "9.9";
        result = userService.findVerify("40", user, null, null, "20", curPaymentAmount);
        verifyCount = (Integer) result.get("verifyCount");
        verifyName = "9.9体验套餐";
        if (verifyCount > 0) {
            type = new HashMap<>();
            type.put("useMoney", false);
            if (verifyCount == 1) {
                curPaymentAmount = "0";
                verifyName = "剩余" + verifyCount + "次";
            } else {
                verifyCount = 0;
                type.put("useMoney", true);
            }
            type.put("name", "￥9.9");
            type.put("method", 60);
            type.put("paymentAmount", curPaymentAmount);
            type.put("verifyCount", verifyCount);
            type.put("verifyTitle", verifyName);
            typeList.add(type);
        }

        curPaymentAmount = "999";
        result = userService.findVerify("20", user, null, storeId, "20", curPaymentAmount);
        verifyCount = (Integer) result.get("verifyCount");
        verifyName = "未购买";
        type = new HashMap<>();
        type.put("useMoney", false);
        if (verifyCount > 0) {
            verifyName = "剩余" + verifyCount + "次";
            curPaymentAmount = "0";
        } else {
            type.put("useMoney", true);
        }
        type.put("name", "￥999");
        type.put("method", 63);
        type.put("paymentAmount", curPaymentAmount);
        type.put("verifyCount", verifyCount);
        type.put("verifyTitle", "能量舱3次包");
        type.put("verifyName", verifyName);
        typeList.add(type);

        curPaymentAmount = "3999";
        result = userService.findVerify("20", user, null, storeId, "20", curPaymentAmount);
        verifyCount = (Integer) result.get("verifyCount");
        verifyName = "未购买";
        curPaymentAmount = "3999";
        type = new HashMap<>();
        type.put("useMoney", false);
        if (verifyCount > 0) {
            verifyName = "剩余" + verifyCount + "次";
            curPaymentAmount = "0";
        } else {
            type.put("useMoney", true);
        }
        type.put("name", "￥3999");
        type.put("method", 61);
        type.put("paymentAmount", curPaymentAmount);
        type.put("verifyCount", verifyCount);
        type.put("verifyTitle", "能量舱20次包");
        type.put("verifyName", verifyName);
        typeList.add(type);

        curPaymentAmount = "5000";
        result = userService.findVerify("20", user, null, storeId, "20", curPaymentAmount);
        verifyCount = (Integer) result.get("verifyCount");
        verifyName = "未购买";
        type = new HashMap<>();
        type.put("useMoney", false);
        if (verifyCount > 0) {
            verifyName = "剩余" + verifyCount + "次";
            curPaymentAmount = "0";
        } else {
            type.put("useMoney", true);
        }
        type.put("name", "￥5000");
        type.put("method", 62);
        type.put("paymentAmount", curPaymentAmount);
        type.put("verifyCount", verifyCount);
        type.put("verifyTitle", "能量舱10次包");
        type.put("verifyName", verifyName);
        typeList.add(type);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("typeList", typeList);
        return ResultVo.success(resultMap, "成功");
    }

    @ApiOperation(notes = "submit", httpMethod = "POST", value = "订单提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
            @ApiImplicitParam(name = "storeId", value = "店铺ID", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "method", value = "方式", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "paymentAmount", value = "支付金额，单位为元，2位小数", required = false, paramType = "query", dataType = "BigDecimal"),
            @ApiImplicitParam(name = "storeGuideId", value = "美导ID", required = false, paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "submit", method = RequestMethod.POST)
    public ResultVo submit(Integer method, Long storeId, BigDecimal paymentAmount, Long storeGuideId, HttpServletRequest request, HttpServletResponse response) {
        String userToken = request.getHeader(UserUtils.USER_TOKEN);
        String key = RedisConstant.USER_TOKEN_KEY + userToken;
        User user = (User) RedisUtil.get(key);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
        }
        Long userId = user.getId();
        if (storeId == null) {
            return ResultVo.error(ResultCode.ERROR, "店铺ID不能为空");
        }
        if (method == null) {
            return ResultVo.error(ResultCode.ERROR, "方式不能为空");
        }
        ShopUser shopUser = shopUserService.findByStoreId(storeId);
        Long shopUserId = shopUser.getId();
        String orderNumber = facePayStoreOrderService.buildOrderNumber();
        FacePayStoreOrder facePayStoreOrder = new FacePayStoreOrder();
        facePayStoreOrder.setStoreId(storeId);
        facePayStoreOrder.setStoreManagerId(shopUserId);
        facePayStoreOrder.setUserId(userId);
        facePayStoreOrder.setMoney(paymentAmount);
        facePayStoreOrder.setOrderNumber(orderNumber);
        facePayStoreOrder.setPayStatus(10);
        facePayStoreOrder.setCommissionStatus(10);
        facePayStoreOrder.setHasVerifyCount(0);
        facePayStoreOrder.setTakenewHasVerifyCount(0);
        if (storeGuideId != null) {
            StoreGuide storeGuide = storeGuideService.findById(storeGuideId);
            if (storeGuide == null) {
                return ResultVo.error(ResultCode.ERROR, "美导ID错误");
            }
            facePayStoreOrder.setGuideUserId(storeGuide.getUserId());
        }
        if (method == 60) {
            Map<String, Object> verifyResult = userService.findVerify("40", user, null, storeId, "20", "9.9");
            Integer verifyCount = (Integer) verifyResult.get("verifyCount");
            if (verifyCount > 0) {
                return ResultVo.error(ResultCode.ERROR, "已购买过9.9体验套餐");
            } else {
                facePayStoreOrder.setMemo("9.9体验套餐");
                facePayStoreOrder.setPayType("40");
                facePayStoreOrder.setCanVerifyCount(1);
                facePayStoreOrder.setTakenewVerifyCount(0);
                facePayStoreOrderService.insert(facePayStoreOrder);
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("orderNumber", orderNumber);
                return ResultVo.success(resultMap, "成功");
            }
        } else if (method == 61) {
            facePayStoreOrder.setMemo("充值3999元");
            facePayStoreOrder.setPayType("20");
            facePayStoreOrder.setCanVerifyCount(20);
            facePayStoreOrder.setTakenewVerifyCount(5);
            facePayStoreOrderService.insert(facePayStoreOrder);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("orderNumber", orderNumber);
            return ResultVo.success(resultMap, "成功");
        } else if (method == 62) {
            facePayStoreOrder.setMemo("充值5000元");
            facePayStoreOrder.setPayType("20");
            facePayStoreOrder.setCanVerifyCount(10);
            facePayStoreOrder.setTakenewVerifyCount(5);
            facePayStoreOrderService.insert(facePayStoreOrder);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("orderNumber", orderNumber);
            return ResultVo.success(resultMap, "成功");
        } else if (method == 63) {
            facePayStoreOrder.setMemo("充值999元");
            facePayStoreOrder.setPayType("20");
            facePayStoreOrder.setCanVerifyCount(3);
            facePayStoreOrder.setTakenewVerifyCount(0);
            facePayStoreOrderService.insert(facePayStoreOrder);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("orderNumber", orderNumber);
            return ResultVo.success(resultMap, "成功");
        } else {
            return ResultVo.error(ResultCode.ERROR, "方式不存在,请重新方式");
        }
    }

    @ApiOperation(notes = "saveOrder", httpMethod = "POST", value = "订单提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
            @ApiImplicitParam(name = "storeId", value = "店铺ID", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "goodsId", value = "商品id", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "paymentAmount", value = "支付金额，单位为元", required = false, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "storeGuideId", value = "美导ID", required = false, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "remark", value = "备注", required = false, paramType = "query", dataType = "string"),
    })
    @RequestMapping(value = "saveOrder", method = RequestMethod.POST)
    public ResultVo saveOrder(String goodsId, String storeId, String paymentAmount, String storeGuideId, String remark, HttpServletRequest request, HttpServletResponse response) {
        String userToken = request.getHeader(UserUtils.USER_TOKEN);
        String key = RedisConstant.USER_TOKEN_KEY + userToken;
        User user = (User) RedisUtil.get(key);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
        }
        Long userId = user.getId();
        if (storeId == null) {
            return ResultVo.error(ResultCode.ERROR, "店铺ID不能为空");
        }
        if (goodsId == null) {
            return ResultVo.error(ResultCode.ERROR, "商品id不能为空");
        }
        ShopUser shopUser = shopUserService.findByStoreId(Long.valueOf(storeId));
        Long shopUserId = shopUser.getId();
        String orderNumber = facePayStoreOrderService.buildOrderNumber();
        FacePayStoreOrder facePayStoreOrder = new FacePayStoreOrder();
        facePayStoreOrder.setStoreId(Long.valueOf(storeId));
        facePayStoreOrder.setStoreManagerId(shopUserId);
        facePayStoreOrder.setUserId(userId);
        facePayStoreOrder.setMoney(StringUtils.isNotBlank(paymentAmount) ? new BigDecimal(paymentAmount) : BigDecimal.ZERO);
        facePayStoreOrder.setOrderNumber(orderNumber);
        facePayStoreOrder.setPayStatus(10);
        facePayStoreOrder.setCommissionStatus(10);
        facePayStoreOrder.setHasVerifyCount(0);
        facePayStoreOrder.setTakenewHasVerifyCount(0);
        facePayStoreOrder.setRemark(remark);
        if (StringUtils.isNotBlank(storeGuideId)) {
            StoreGuide storeGuide = storeGuideService.findById(Long.valueOf(storeGuideId));
            if (storeGuide == null) {
                return ResultVo.error(ResultCode.ERROR, "美导ID错误");
            }
            facePayStoreOrder.setGuideUserId(storeGuide.getUserId());
        }
        if ("153".equals(goodsId)) {
            //Map<String, Object> verifyResult = userService.findVerify("40", user, null, Long.valueOf(storeId), "20", "9.9");
//            Map<String, Object> verifyResult = userService.findVerify("40", user, null, null, "20", "9.9");
//            Integer verifyCount = (Integer) verifyResult.get("verifyCount");
//            // 兼容之前重复购买的问题
//            if (verifyCount == 1 || verifyCount > 2 || verifyResult.get("canBuy") == null) {
//                return ResultVo.error(ResultCode.ERROR, "已购买过9.9体验套餐");
//            } else {
//                facePayStoreOrder.setMemo("9.9体验套餐");
//                facePayStoreOrder.setPayType("40");
//                facePayStoreOrder.setCanVerifyCount(1);
//                facePayStoreOrder.setTakenewVerifyCount(0);
//                facePayStoreOrderService.insert(facePayStoreOrder);
//                Map<String, Object> resultMap = new HashMap<>();
//                resultMap.put("orderNumber", orderNumber);
//                return ResultVo.success(resultMap, "成功");
//            }
            Map<String, Object> verifyResult = userService.findVerify("40", user, null, null, "20", "29.9");
            Integer verifyCount = (Integer) verifyResult.get("verifyCount");
            // 兼容之前重复购买的问题
            if (verifyCount == 1 || verifyCount > 2 || verifyResult.get("canBuy") == null) {
                return ResultVo.error(ResultCode.ERROR, "已购买过29.9体验套餐");
            } else {
                // 线下门店引流体验套餐 29.9体验套餐与99疼痛体验套餐只能二选一
                List<FacePayStoreOrder> facePayStoreOrders = facePayStoreOrderService.findByParams(null, null, userId, null, "20", "20", "99");
                if (!CollectionUtils.isEmpty(facePayStoreOrders)) {
                    return ResultVo.error(ResultCode.ERROR, "线下门店引流体验套餐只能二选一,已购买过99疼痛体验套餐");
                }
                List<FacePayStoreOrder> facePayStoreOrders98 = facePayStoreOrderService.findByParams(null, null, userId, null, "20", "20", "98");
                if (!CollectionUtils.isEmpty(facePayStoreOrders98)) {
                    return ResultVo.error(ResultCode.ERROR, "线下门店引流体验套餐只能二选一,已购买过98燃脂套餐");
                }
                facePayStoreOrder.setMemo("29.9体验套餐");
                facePayStoreOrder.setPayType("40");
                facePayStoreOrder.setCanVerifyCount(1);
                facePayStoreOrder.setTakenewVerifyCount(0);
                facePayStoreOrderService.insert(facePayStoreOrder);
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("orderNumber", orderNumber);
                return ResultVo.success(resultMap, "成功");
            }
        } else if ("154".equals(goodsId)) {
            facePayStoreOrder.setMemo("充值3999元");
            facePayStoreOrder.setPayType("20");
            facePayStoreOrder.setCanVerifyCount(20);
            facePayStoreOrder.setTakenewVerifyCount(5);
            facePayStoreOrderService.insert(facePayStoreOrder);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("orderNumber", orderNumber);
            return ResultVo.success(resultMap, "成功");
        } else if ("155".equals(goodsId)) {
            facePayStoreOrder.setMemo("充值9999元");
            facePayStoreOrder.setPayType("20");
            facePayStoreOrder.setCanVerifyCount(60);
            facePayStoreOrder.setTakenewVerifyCount(5);
            facePayStoreOrderService.insert(facePayStoreOrder);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("orderNumber", orderNumber);
            return ResultVo.success(resultMap, "成功");
        } else if ("156".equals(goodsId)) {
            // 每个月只能购买一次
            List<FacePayStoreOrder> facePayStoreOrders = facePayStoreOrderService.findByParams(DateUtils.generateMonthDayBegin(), DateUtils.generateMonthDayEnd(), user.getId(), Long.valueOf(storeId), "20", "20", "399");
            if (!CollectionUtils.isEmpty(facePayStoreOrders)) {
                return ResultVo.error(ResultCode.ERROR, "本月已购买过399套餐，无法再购买");
            }
            facePayStoreOrder.setMemo("充值399元");
            facePayStoreOrder.setPayType("20");
            facePayStoreOrder.setCanVerifyCount(30);
            facePayStoreOrder.setTakenewVerifyCount(0);
            facePayStoreOrderService.insert(facePayStoreOrder);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("orderNumber", orderNumber);
            return ResultVo.success(resultMap, "成功");
        } else if ("157".equals(goodsId)) {
            facePayStoreOrder.setMemo("充值5000元");
            facePayStoreOrder.setPayType("20");
            //facePayStoreOrder.setCanVerifyCount(10);
            facePayStoreOrder.setCanVerifyCount(20);
            facePayStoreOrder.setTakenewVerifyCount(0);
            facePayStoreOrderService.insert(facePayStoreOrder);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("orderNumber", orderNumber);
            return ResultVo.success(resultMap, "成功");
        } else if ("158".equals(goodsId)) {
            facePayStoreOrder.setMemo("充值999元");
            facePayStoreOrder.setPayType("20");
            facePayStoreOrder.setCanVerifyCount(3);
            facePayStoreOrder.setTakenewVerifyCount(0);
            facePayStoreOrderService.insert(facePayStoreOrder);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("orderNumber", orderNumber);
            return ResultVo.success(resultMap, "成功");
        } else {
            // 这边加个根据goodsId查商品，获取到关键参数的值，这样子不用每次后台加商品这边就要做处理
            JwShoproGoods goods = jwShoproGoodsService.findById(Long.valueOf(goodsId));
            if (goods == null) {
                return ResultVo.error(ResultCode.ERROR, "商品id不存在,请重新方式");
            }
            if (goodsId.equals("168")) {
                // 限制只能买1次149的
                List<FacePayStoreOrder> facePayStoreOrders = facePayStoreOrderService.findByParams(null, null, user.getId(), null, "20", "20", "149");
                if (!CollectionUtils.isEmpty(facePayStoreOrders)) {
                    return ResultVo.error(ResultCode.ERROR, "已购买过149敬老爱心套餐，无法再购买");
                }
                facePayStoreOrders = facePayStoreOrderService.findByParams(null, null, user.getId(), null, "20", "20", "199");
                if (!CollectionUtils.isEmpty(facePayStoreOrders)) {
                    return ResultVo.error(ResultCode.ERROR, "已购买过199敬老爱心套餐，无法再购买");
                }
            }
            // 线下门店引流体验套餐 29.9体验套餐与99疼痛体验套餐只能二选一
            if (goodsId.equals("174")) {
                // 与29.9体验套餐只能二选一
                List<FacePayStoreOrder> facePayStoreOrders = facePayStoreOrderService.findByParams(null, null, user.getId(), null, "20", "20", "99");
                if (!CollectionUtils.isEmpty(facePayStoreOrders)) {
                    return ResultVo.error(ResultCode.ERROR, "已购买过超值疼痛体验套餐，无法再购买");
                }
                facePayStoreOrders = facePayStoreOrderService.findByParams(null, null, user.getId(), null, "20", "20", "29.9");
                if (!CollectionUtils.isEmpty(facePayStoreOrders)) {
                    return ResultVo.error(ResultCode.ERROR, "线下门店引流体验套餐只能二选一,已购买过29.9体验套餐，无法购买");
                }
            }
            if (goodsId.equals("178")) {
                // 与29.9体验套餐只能二选一
                List<FacePayStoreOrder> facePayStoreOrders = facePayStoreOrderService.findByParams(null, null, userId, null, "20", "20", "98");
                if (!CollectionUtils.isEmpty(facePayStoreOrders)) {
                    return ResultVo.error(ResultCode.ERROR, "已购买过超值燃脂体验套餐，无法再购买");
                }
                facePayStoreOrders = facePayStoreOrderService.findByParams(null, null, user.getId(), null, "20", "20", "29.9");
                if (!CollectionUtils.isEmpty(facePayStoreOrders)) {
                    return ResultVo.error(ResultCode.ERROR, "线下门店引流体验套餐只能二选一,已购买过29.9体验套餐，无法购买");
                }
            }
            facePayStoreOrder.setMemo("充值" + goods.getPrice() + "元");
            facePayStoreOrder.setPayType("20");
            Integer canVerifyCount = getValueByReg(goods.getTitle());
            // TODO: 判断下线下门店消费合伙人套餐：3000元
            // 开业前：（以设备第一次开机使用之前为界限）可使用30次能量舱
            // 开业后：（以设备第一次开机使用之后为界限) 可使用20次能量舱
            if (goodsId.equals("171")) {
                // 查询该门店是否已经有开过机
                List<FacePayStoreConsumeLog> list = facePayStoreConsumeLogService.findList(null, Long.valueOf(storeId), null);
                if (CollectionUtils.isEmpty(list)) {
                    canVerifyCount = 30;
                } else {
                    canVerifyCount = 20;
                }
            }
            facePayStoreOrder.setCanVerifyCount(canVerifyCount);
            facePayStoreOrder.setTakenewVerifyCount(0);
            facePayStoreOrderService.insert(facePayStoreOrder);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("orderNumber", orderNumber);
            return ResultVo.success(resultMap, "成功");
        }
    }

    public static Integer getValueByReg(String title) {
        try {
            String regEx = "[^0-9]";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(title);
            return Integer.valueOf(m.replaceAll("").trim());
        } catch (Throwable e) {

        }
        return 0;
    }
}
